home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Overload Trio 2
/
Shareware Overload Trio Volume 2 (Chestnut CD-ROM).ISO
/
dir30
/
alsps.zip
/
ALSP2.DOC
< prev
next >
Wrap
Text File
|
1993-11-04
|
8KB
|
256 lines
This is Lesson 2 of a series of AutoLISP training exercises given
on the CompuServe ADESK Forum by the Autodesk, Inc. Training
Department.
EVERY OBJECT HAS A VALUE
There are two types of objects in AutoLISP; atoms and lists.
Every atom and every list has a value. The process by which
AutoLISP determines the value of an object is called
"evaluation".
EVALUATION OF ATOMS
Atoms are simple objects, and they are evaluated using simple
rules according to their data type. There are other data types
apart from those listed below; they will be examined in detail in
the next lesson. The value of a symbol may not be obvious until
the process of "binding" is discussed along with the evaluation
of lists.
Type of Atom Example Evaluation rule Value
Integer 1 Value is itself 1
Real 4.5 Value is itself 4.5
String "text" Value is itself "text"
Symbol X Current binding Whatever was
last assigned
EVALUATION OF LISTS
The value of a list can generally be determined in one of two
ways; either by taking the list at face value, or by evaluating
the list. One method will generally yield different results, or
a different value, than the other.
If a list is taken at face value, then its value is itself. This
will become somewhat clearer upon explanation of the (quote)
function later in this lesson.
Lists are evaluated according to the first element of the list.
If the first element is the name of an internal AutoLISP
function, or subr, the remaining list elements are passed to the
subr as the formal arguments and are evaluated by the subr.
For example, this is a list of three elements, the subr + and the
integers 1 and 2.
(+ 1 2)
This is the evaluation process by which AutoLISP determines the
value of this list. All evaluations of the same list occur in
exactly the same manner, every time. The same general process of
evaluation is applied to every list that can be and is evaluated
in exactly the same manner, every time.
(+ ..
AutoLISP looks at the first element of the list, where it expects
to find a function. Functions are either internal AutoLISP
functions, or subrs, such as those listed in Chapter 4, "AutoLISP
Functions", of the APR, or user-defined functions created by the
(defun) or (lambda) functions.
AutoLISP evaluates the function, or first element of the list. A
function evaluates to a set of instructions that tells AutoLISP
what to do next.
(Isn't that elegant? Every complex expression in AutoLISP
carries its own processing instructions with it, in exactly the
same place every time; at the head of the list.)
In this case the function + evaluates to a set of instructions
that tells AutoLISP, "Go find the value of every other element in
this list. When you're done, add all the values together and
return the result as the value of the entire list."
(.. 1 ..)
Following its instructions, AutoLISP finds the value of the next
element in the list, the atom and integer 1. Integers evaluate
to themselves, so AutoLISP stores the value 1 and continues with
its instructions.
(.. .. 2)
The value of the integer 2 is 2. AutoLISP stores this value and
continues.
Elements in a list following a function are arguments to the
function; that is, they are the data upon which the function
operates. Finding no more elements in the list, or arguments to
the function, AutoLISP finishes the instructions it received by
evaluating the function and adds the values of the arguments,
returning the integer 3 as the value of the list.
Command: (+ 1 2)
3
A MORE COMPLEX EXAMPLE
AutoLISP (any Lisp, in fact) always evaluates the elements in a
list from left to right. You may have read that elements are
evaluated in a nested manner, from the inside out; this is wrong.
It may be convenient for us in some cases to follow the
evaluation process by working from the inside out, but that is
never the way AutoLISP evaluates a list. It always works from
left to right.
Consider this list of three elements, the function (or subr) +,
the atom 1, and the list (+ 2 3).
(+ 1 (+ 2 3))
This is how AutoLISP evaluates this list.
(+ ..
AutoLISP looks at the first element of the list and evaluates it.
Since it's a function, it returns as its value a set of
instructions on what AutoLISP should do next.
(.. 1 ..
Following the instructions of the + function, AutoLISP finds the
value of the second element in the list (the first argument to
the + function), the atom 1, stores that value, and continues.
(.. .. (+ 2 3)
AutoLISP finds the value of the third element in the list, the
second argument to the + function. The third element is itself a
list. How does AutoLISP evaluate a list?
First, it evaluates the function + and receives its instructions.
Next, it finds the values of the arguments, adds them together,
and returns the result, which in this case is the integer 5.
Finding no more elements in the list, AutoLISP finishes its
instructions by adding the value of the two elements together and
returning the value of the integer 6.
(+ 1 (+ 2 3))
| \ /
| \ /
| |------------ Value of object is integer 5
|----------------- Value of object is integer 1
TAKING A LIST AT FACE VALUE
The function (quote) may be used to return a list (or an atom)
unevaluated; that is, at face value. (quote) takes a single
argument, and returns as its value the argument unevaluated.
For example, this expression returns the list (1.0 2.0 3.0) at
face value.
Command: (quote (1.0 2.0 3.0))
(1.0 2.0 3.0)
What would happen if AutoLISP were asked to evaluate the list?
Why?
Command: (1.0 2.0 3.0)
?
BINDING A SYMBOL TO A VALUE
Binding is the process by which a value is assigned to a
user-defined symbol, or variable. In AutoLISP, we say that a
symbol (or variable) is bound to a value. This is comparable to
saying in the language BASIC that a value is equal to a variable.
The function (setq) is generally used to bind a symbol (or
variable) to a value. For example, this expression binds the
symbol X to a value of 4.5.
Command: (setq x 4.5)
4.5
Command: !x
4.5
In BASIC, the same operation might be expressed as:
LET X = 4.5
The value of the subr (setq) is a set of instructions that boils
down to this:
Find the value of the second argument to setq (the third element
in the list). Take the first argument as is; don't evaluate it.
Bind the first argument to the value of the second argument.
EXERCISE
Which of these lists can legally be evaluated and which must be
taken at face value?
1. (+ 1 2)
2. (+ 1 (+ 2 3))
3. (1.0 2.0 3.0)
4. (quote (1.0 2.0 3.0))
5. (setq x 4.5)
6. (setq y (1.0 2.0 3.0))
Using the functions +, -, /, and * as documented in the APR,
convert the following algebraic expressions into AutoLISP
expressions. For example, the expression:
1 + 2
would be converted into the AutoLISP expression:
(+ 1 2)
1. 3 + 10 + 5
2. 20 * 15
3. 16 - 10
4. 15 / 3
5. 5 + (10 * 2)
6. (5 + 10) * 2
Using the (setq) function and the (quote) function (as needed),
bind the variable X to the following values. Check the value of
the variable by using ! to find its current binding.
1. 1
2. 4.5
3. "text"
4. (1.0 2.0 3.0)
Next week: Data Types
Answers to Lesson 1 Exercises:
1. atom
2. atom
3. list
4. atom
5. list
6. atom and list
1. 4.5
2. "text"
3. 17
4. <Subr: xxxx>
5. nil
6. nil
1. 3
2. 3
3. 3
4. 5
5. 4
6. none